const express = require('express')
const router = express.Router()
const passport = require('passport')
const sd = require('silly-datetime')
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/nodeData';
const ObjectId = require('mongodb').ObjectId

//测试
router.get('/test',(req,res)=>{
    res.json({msg:"hello profile"})
})

//问题无法判断添加和删除  成功与否

//添加
router.post('/add',passport.authenticate("jwt",{session:false}),(req,res)=>{
    var profiles = {};
    if(req.body.type) profiles.type = req.body.type;
    if(req.body.describe) profiles.describe = req.body.describe;
    if(req.body.income) profiles.income = Number(req.body.income);
    if(req.body.expend) profiles.expend = Number(req.body.expend);
    if(req.body.cash) profiles.cash = Number(req.body.cash);
    if(req.body.remark) profiles.remark = req.body.remark;
    profiles.date = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');
    console.log("添加的数据：",profiles)
    MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {
        var dbo =  db.db('nodeData');
        dbo.collection('profile').insert(profiles,(error,reqon)=>{
            if(err){
                res.status(404).json({msg:"没有此信息"})
            }else{
                res.status(200).json({msg:"添加成功"})
            }
        });
        db.close();
    });
})

//获取所有信息
router.get('/queryAll',passport.authenticate("jwt",{session:false}),(req,res)=>{
    MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {
        var dbo =  db.db('nodeData');
        dbo.collection('profile').find().toArray((error,docs)=>{
            if(!docs){
                res.status(404).json({msg:"没有任何内容"})
            }else{
                res.status(200).json({data:docs})
            }
            console.log("查询全部：",docs);
        })
        db.close();
    });
});

//获取单个信息
router.get('/queryOne/:id',passport.authenticate("jwt",{session:false}),(req,res)=>{
    MongoClient.connect(url,{useNewUrlParser:true},(err,db)=>{
        var dbo = db.db('nodeData');
        dbo.collection('profile').findOne({"_id":ObjectId(req.params.id)},(error,docs)=>{
            console.log("查询单个：",docs)
            if(!docs){
                res.status(404).json({msg:"没有此信息"})
            }else{
                res.status(200).json({data:docs})
            }
        })
        db.close();
    })
});

//编辑
router.post('/edit/:id',passport.authenticate("jwt",{session:false}),(req,res)=>{
    var profiles = {};
    if(req.body.type) profiles.type = req.body.type;
    if(req.body.describe) profiles.describe = req.body.describe;
    if(req.body.income) profiles.income = Number(req.body.income);
    if(req.body.expend) profiles.expend = Number(req.body.expend);
    if(req.body.cash) profiles.cash = Number(req.body.cash);
    if(req.body.remark) profiles.remark = req.body.remark;
    profiles.date = sd.format(new Date(), 'YYYY-MM-DD HH:mm:ss');

    console.log("修改的数据：",profiles)

    MongoClient.connect(url, { useNewUrlParser: true }, function (err, db) {
        var dbo =  db.db('nodeData');
        dbo.collection('profile').updateOne({"_id":ObjectId(req.params.id)},{$set:profiles},(error,reqon)=>{
            res.status(200).json({msg:"修改成功"})
        });
        db.close();
    });
});

//删除信息
router.get('/deleteOne/:id',passport.authenticate("jwt",{session:false}),(req,res)=>{
    MongoClient.connect(url,{useNewUrlParser:true},(err,db)=>{
        var dbo = db.db('nodeData');
        dbo.collection('profile').deleteOne({"_id":ObjectId(req.params.id)},(error,docs)=>{
            if(err){
                res.status(404).json({msg:"没有此信息"})
            }else{
                res.status(200).json({msg:"删除成功"})
            }
        })
        db.close();
    })
});


module.exports = router;